package ar.com.larctest.forms;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import ar.com.larctest.R;
import ar.com.larctest.dbHelper.SQLLARCTESTHelper;
import ar.com.larctest.entities.Horses;
import ar.com.larctest.providers.HorsesProvider;
import ar.com.larctest.utils.AppGlobalVariables;
import ar.com.larctest.utils.Constants;

public class PopUpHorseAddActivity extends Activity {
    private static final String TAG = "PopUpHorseAddActivity";

    EditText etName;
    EditText etYearsOls;
    EditText etHair;
    EditText etTemperament;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_up_horse_add);

        etName = (EditText) findViewById(R.id.horseName);
        etYearsOls  = (EditText) findViewById(R.id.horseYearsOld);
        etHair  = (EditText) findViewById(R.id.horseHair);
        etTemperament  = (EditText) findViewById(R.id.horseTemperament);
    }

    public void onClickSave(View view)
    {
        etName = (EditText) findViewById(R.id.horseName);
        etYearsOls  = (EditText) findViewById(R.id.horseYearsOld);
        etHair  = (EditText) findViewById(R.id.horseHair);
        etTemperament  = (EditText) findViewById(R.id.horseTemperament);

        SQLLARCTESTHelper sqlHelper = SQLLARCTESTHelper.getHelper(this);

        if(etName.getText().toString().equals("")) {
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText(getText(R.string.Horses_Add_Required_Name));

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
        else if(etYearsOls.getText().toString().equals("")) {
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText(getText(R.string.Horses_Add_Required_Hair));

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
        else if(etTemperament.getText().toString().equals("")) {
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText(getText(R.string.Horses_Add_Required_Temperament));

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
        else if(etHair.getText().toString().equals("")) {
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText(getText(R.string.Measurement_Required_RespiratoryRate));

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
        else
        {
            sqlHelper.dbOpen();

            try
            {
                sqlHelper.beginTransaction();

                HorsesProvider horsesProvider = new HorsesProvider(this);
                Horses horse = new Horses();
                horse.setName(etName.getText().toString());
                horse.setHair(etHair.getText().toString());
                horse.setYearsOld(Integer.parseInt(etYearsOls.getText().toString()));
                horse.setTemperament(etTemperament.getText().toString());
                horse.setIdUser(AppGlobalVariables.getInstance().getIdUser());
                horse.setState(Constants.NOT_SYNC_RECORD);

                horsesProvider.insertHorse(horse);

                sqlHelper.commitTransaction();

                setResult(RESULT_OK);
                this.finish();
            }
            catch (Exception e) {
                sqlHelper.rollbackTransaction();
                Log.e(TAG, e.toString());
            }
            finally {
                sqlHelper.dbClose();
            }
        }
    }

    public void onClickExit(View view)
    {
        setResult(RESULT_CANCELED);
        this.finish();
    }
}
